package cn.neusoft.service.impl;

import cn.neusoft.mapper.BusinessMapper;
import cn.neusoft.pojo.Business;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IBusinessService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BusinessService implements IBusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Transactional  //spring事务处理
    @Override
    public ResultBean add(Business business) throws Exception {

        //参数校验
        if (business==null ||
            StringUtils.isEmpty(business.getBusinessAccount()) ||
            StringUtils.isEmpty(business.getBusinessName())){
            return ResultBean.ERROR(10001,"params null");
        }

        int i = businessMapper.add(business);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("add business fail");
    }

    @Transactional
    @Override
    public ResultBean update(Business business) throws Exception {
        //参数校验
        if (business==null ||
            business.getBusinessId()==null ||
            StringUtils.isEmpty(business.getBusinessAccount()) ||
            StringUtils.isEmpty(business.getBusinessName())){
            return ResultBean.ERROR(10001,"params null");
        }

        int i = businessMapper.update(business);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("update business fail");
    }


    @Transactional
    @Override
    public ResultBean delete(Integer id) throws Exception {
        //参数校验
        if (id==null){
            return ResultBean.ERROR(10001,"params null");
        }

        int i = businessMapper.delete(id);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("delete business fail");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) throws Exception {
        //参数校验
        if (id==null){
            return ResultBean.ERROR(10001,"params null");
        }
        Business business = businessMapper.getById(id);
        return business!=null?ResultBean.SUCCESS(business):ResultBean.ERROR("get business by id fail");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() throws Exception {
        List<Business> businessList = businessMapper.getAll();
        return businessList!=null?ResultBean.SUCCESS(businessList):ResultBean.ERROR("get all business fail");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {

        PageBean<Business> pageBean = new PageBean<>();

        //查询总记录
        int count = businessMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        //计算总页数
        if(count% baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else{
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }

        //查询集合
        List<Business> businessList = businessMapper.getByPage(baseQuery);
        pageBean.setData(businessList);

        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids) throws Exception {

        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("params null");
        }

        for (Integer id:ids){
            businessMapper.delete(id);
        }

        return ResultBean.SUCCESS();
    }
}
